package my.Views;

import java.util.ArrayList;

import my.eightZOne.R;
import my.eightZOne.explainActivity;
import my.eightZOne.playActivity;
import my.eightZOne.rankActivity;
import my.eightZOne.treasureActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

public class MenuView extends View {

    //刀锋效果begin
    public static final int MESSAGE_CLIP = 100;
    public static final int REFRESH = 20;
    private Paint mPaint;
    private Path mPath;
    private int pointCount = 10;
    private int index = 0;
    private float offset = 20;
    private ArrayList<PointF> eventPointList = new ArrayList<PointF>();
    private ArrayList<PointF> pointPath = new ArrayList<PointF>();
    private CornerPathEffect mCornerPathEffect = new CornerPathEffect(10);
    //刀锋效果end
    private Canvas canvasOut;                        //定义画布
    private final Paint paint = new Paint();        //定义画笔

    private float Height;        //屏幕宽度与预设比例
    private float Width;        //屏幕高度与预设比例

    private int widthButtom;        //主按钮宽
    private int heightButtom;        //主按钮高

    private int flag_buttom = 0;        //被点击的按钮标识
    MediaPlayer button = MediaPlayer.create(this.getContext(), R.raw.button);            //按钮音效


    //图片引入
    Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.welcome);
    Bitmap eightModeDown = BitmapFactory.decodeResource(getResources(), R.drawable.eightmodedown);
    Bitmap eightModeUp = BitmapFactory.decodeResource(getResources(), R.drawable.eightmodeup);
    Bitmap fiveModeUp = BitmapFactory.decodeResource(getResources(), R.drawable.fivemodeup);
    Bitmap fiveModeDown = BitmapFactory.decodeResource(getResources(), R.drawable.fivemodedown);

    Bitmap exitDown = BitmapFactory.decodeResource(getResources(), R.drawable.exitdown);
    Bitmap exitUp = BitmapFactory.decodeResource(getResources(), R.drawable.exitup);
    Bitmap getgradeUp = BitmapFactory.decodeResource(getResources(), R.drawable.getgradeup);
    Bitmap getgradeDown = BitmapFactory.decodeResource(getResources(), R.drawable.getgradedown);

    Bitmap rankDown = BitmapFactory.decodeResource(getResources(), R.drawable.rankdown);
    Bitmap rankUp = BitmapFactory.decodeResource(getResources(), R.drawable.rankup);

    Bitmap backgroundF;                        //背景图片
    Bitmap eightModeDownF, eightModeUpF;        //缩放后图片
    Bitmap fiveModeUpF, fiveModeDownF;
    Bitmap exitUpF, exitDownF;
    Bitmap getgradeUpF, getgradeDownF;
    Bitmap rankUpF, rankDownF;

    public MenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        setFocusable(true);        //设置焦点调用系统监听

        init();                    //刀锋效果初始化

        DisplayMetrics dm = getResources().getDisplayMetrics();
        Width = (float) dm.widthPixels / 480;        //屏幕宽度与预设比例
        Height = (float) dm.heightPixels / 800;    //屏幕高度与预设比例


        int bgWidth = background.getWidth();
        int bgHeight = background.getHeight();
        backgroundF = scaleImg(background, Width * 480 / bgWidth, Height * 800 / bgHeight);

        eightModeDownF = scaleImg(eightModeDown, Height);        //newBm为缩放后图片
        eightModeUpF = scaleImg(eightModeUp, Height);

        fiveModeUpF = scaleImg(fiveModeUp, Height);
        fiveModeDownF = scaleImg(fiveModeDown, Height);

        exitUpF = scaleImg(exitUp, Height);
        exitDownF = scaleImg(exitDown, Height);

        getgradeUpF = scaleImg(getgradeUp, Height);
        getgradeDownF = scaleImg(getgradeDown, Height);

        rankUpF = scaleImg(rankUp, Height);
        rankDownF = scaleImg(rankDown, Height);
    }

    //重写父类绘图函数
    @Override
    protected void onDraw(Canvas canvas) {

        canvasOut = canvas;
        canvasOut.drawBitmap(backgroundF, 0, 0, paint);        //绘制背景

        widthButtom = rankUpF.getWidth();
        heightButtom = rankUpF.getHeight();

        //绘制静态按钮
        canvasOut.drawBitmap(eightModeUpF, 180 * Width, 260 * Height, paint);
        canvasOut.drawBitmap(fiveModeUpF, 110 * Width, 360 * Height, paint);
        canvasOut.drawBitmap(rankUpF, 40 * Width, 460 * Height, paint);
        canvasOut.drawBitmap(getgradeUpF, 110 * Width, 560 * Height, paint);
        canvasOut.drawBitmap(exitUpF, 180 * Width, 660 * Height, paint);

        //绘制
        switch (flag_buttom) {
            case 1:
                canvasOut.drawBitmap(eightModeDownF, 180 * Width, 260 * Height, paint);
                break;
            case 2:
                //canvasOut.drawBitmap(eightModeUpF, 180*Width, 260*Height, paint);
                break;
            case 3:
                canvasOut.drawBitmap(fiveModeDownF, 110 * Width, 360 * Height, paint);
                break;
            case 4:
                //canvasOut.drawBitmap(fiveModeUpF, 110*Width, 360*Height, paint);
                break;
            case 5:
                canvasOut.drawBitmap(rankDownF, 40 * Width, 460 * Height, paint);
                break;
            case 6:
                //canvasOut.drawBitmap(rankUpF, 40*Width, 460*Height, paint);
                break;
            case 7:
                canvasOut.drawBitmap(getgradeDownF, 110 * Width, 560 * Height, paint);
                break;
            case 8:
                //canvasOut.drawBitmap(getgradeUpF, 110*Width, 560*Height, paint);
                break;
            case 9:
                canvasOut.drawBitmap(exitDownF, 180 * Width, 660 * Height, paint);
                break;
            case 10:
                //canvasOut.drawBitmap(exitUpF, 180*Width, 660*Height, paint);
                break;
            case 0:
                break;
        }

        drawLine(canvas);        //绘制刀锋效果
    }

    //重写触屏事件函数
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX(0);        //获取横坐标
        float y = event.getY(0);        //获取纵坐标

        //刀锋效果 - begin
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                index = 0;
                myHandler.removeMessages(MESSAGE_CLIP);
                index++;
                eventPointList.clear();
                eventPointList.add(new PointF(x, y));
                break;
            case MotionEvent.ACTION_MOVE:

                PointF eventMove = new PointF(x, y);
                if (index > 0 && distance(eventMove, eventPointList.get(index - 1)) > 10.0f) {
                    if (index > pointCount) {
                        eventPointList.add(new PointF(x, y));
                        eventPointList.remove(0);
                    } else {
                        eventPointList.add(new PointF(x, y));
                        index++;
                    }

                } else {
                    if (index > 4) {
                        eventPointList.remove(0);
                        index--;
                    }
                }
                invalidate();

                break;
            case MotionEvent.ACTION_UP:
                myHandler.sendEmptyMessageDelayed(MESSAGE_CLIP, 100);
                break;
        }
        //刀锋效果 - end

        if ((x > 180 * Width && x < 180 * Width + widthButtom) && (y > 260 * Height && y < 260 * Height + heightButtom)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                flag_buttom = 1;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 2;
                button.start();            //按钮音效
                //跳转到棋盘界面
                Intent intent = new Intent(this.getContext(), playActivity.class);
                this.getContext().startActivity(intent);
            }
        } else if ((x > 110 * Width && x < 110 * Width + widthButtom) && (y > 360 * Height && y < 360 * Height + heightButtom)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                flag_buttom = 3;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 4;
                button.start();            //按钮音效
                //进入藏宝阁界面
                Intent intent = new Intent(this.getContext(), treasureActivity.class);
                this.getContext().startActivity(intent);
            }
        } else if ((x > 40 * Width && x < 40 * Width + widthButtom) && (y > 460 * Height && y < 460 * Height + heightButtom)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                flag_buttom = 5;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 6;
                button.start();            //按钮音效
                //进入战绩排行界面
                Intent intent = new Intent(this.getContext(), rankActivity.class);
                this.getContext().startActivity(intent);
            }
        } else if ((x > 110 * Width && x < 110 * Width + widthButtom) && (y > 560 * Height && y < 560 * Height + heightButtom)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                flag_buttom = 7;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 8;
                button.start();                //按钮音效
            }
        } else if ((x > 180 * Width && x < 180 * Width + widthButtom) && (y > 660 * Height && y < 660 * Height + heightButtom)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                flag_buttom = 9;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 10;
                button.start();            //按钮音效
                System.exit(0);        //退出系统
            }
        }

        postInvalidate();        //再次调用onDraw函数重绘画布
        return true;
    }

    //缩放图片函数
    public Bitmap scaleImg(Bitmap bm, float sca) {
        // 获得图片的宽高		
        int width = bm.getWidth();
        int height = bm.getHeight();

        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(sca, sca);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

    //缩放图片函数
    public Bitmap scaleImg(Bitmap bm, float scaW, float scaH) {
        // 获得图片的宽高		
        int width = bm.getWidth();
        int height = bm.getHeight();

        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaW, scaH);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

    //刀锋效果 - 初始化
    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setPathEffect(mCornerPathEffect);
        mPaint.setColor(0xCCFFFF00);
        mPath = new Path();
    }

    //刀锋效果 - 画线
    private void drawLine(Canvas canvas) {

        if (index < 2) {
            return;
        } else {
            float w = offset / ((index < pointCount ? index : pointCount) + 5);
            PointF tempLast = eventPointList.get(index - 1);
            float k = getK(tempLast, eventPointList.get(index - 2));
            PointF lastPoint = new PointF();
            lastPoint.x = (float) (tempLast.x + Math.cos(k) * 10);
            lastPoint.y = (float) (tempLast.y + Math.sin(k) * 10);
            pointPath.add(lastPoint);
            for (int i = index - 2; i > 0; i--) {
                float kLeft = getK(eventPointList.get(i),
                        eventPointList.get(i - 1));
                PointF temp = new PointF();
                temp.x = (float) (eventPointList.get(i).x - Math.sin(kLeft) * w
                        * i);
                temp.y = (float) (eventPointList.get(i).y + Math.cos(kLeft) * w
                        * i);
                pointPath.add(0, temp);
            }
            pointPath.add(0, eventPointList.get(0));
            pointPath.add(lastPoint);
            for (int i = index - 2; i > 0; i--) {
                float kRight = getK(eventPointList.get(i),
                        eventPointList.get(i - 1));
                PointF temp = new PointF();
                temp.x = (float) (eventPointList.get(i).x + Math.sin(kRight) * w
                        * i);
                temp.y = (float) (eventPointList.get(i).y - Math.cos(kRight) * w
                        * i);
                pointPath.add(temp);
            }
            pointPath.add(eventPointList.get(0));
            drawLineByPoints(canvas);

        }
    }

    public float distance(PointF p1, PointF p2) {
        float tt = (float) Math.pow((p1.x - p2.x) * (p1.x - p2.x)
                + (p1.y - p2.y) * (p1.y - p2.y), 0.5);
        return tt;

    }

    public float getK(PointF p1, PointF p2) {
        return (float) Math.atan2(p1.y - p2.y, p1.x - p2.x);
    }

    public void drawLineByPoints(Canvas canvas) {
        int pathSize = index * 2;
        mPath.reset();
        mPath.moveTo(pointPath.get(0).x, pointPath.get(0).y);
        for (int i = 1; i < pathSize; i++) {
            mPath.lineTo(pointPath.get(i).x, pointPath.get(i).y);
        }
        //带填充色的path
        canvas.drawPath(mPath, mPaint);
        //不带填充色的path
        //canvas.drawPath(mPath, mStrokePaint);
        pointPath.clear();

    }

    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {
                case MESSAGE_CLIP:
                    if (index > 1) {
                        eventPointList.remove(0);
                        index--;
                        this.sendEmptyMessageDelayed(MESSAGE_CLIP, REFRESH);
                    } else {
                        index = 0;
                        eventPointList.clear();
                        return;
                    }
                    invalidate();

                    break;
            }

        }

    };

    public void clear() {
        mPath.reset();
        mPath = null;
        eventPointList.clear();
        eventPointList = null;
        eventPointList = null;
        eventPointList = null;
        mCornerPathEffect = null;
        mPaint = null;
        myHandler = null;
    }
    //刀锋效果 - end
}
